#-*- codeing = utf-8 -*-
#@Time : 2020/10/30 16:17
#@Author : 阳某
#@File : 15.python绘制词云.py
#@Software : PyCharm

'''
Python绘制词云图展示射雕英雄传高频词语
使用中文分词库Python库：jieba，自然语言处理必备，https://github.com/fxsjy/jieba
'''

import jieba
import jieba.analyse
import pandas as pd
import pyecharts.options as opts
from pyecharts.charts import WordCloud

# 1、将小说文字分词
fin = open('射雕英雄传.txt', encoding='utf-8')
# 读取txt全部文字
txt = fin.read()
print(txt[:100])
# 使用TF/IDF提取关键词
word_weights = jieba.analyse.extract_tags(
    txt, topK=50, withWeight=True)

print(word_weights[:30])
# 绘制词云
word_cloud = (
    WordCloud()
    .add(series_name="高频词语",
         data_pair=word_weights, word_size_range=[10, 50])
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="射雕英雄传-高频词云图",
            title_textstyle_opts=opts.TextStyleOpts(font_size=23),
            pos_left='center')
    )
)